package com.newland.nxsms.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Properties;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public final class ConfigUtils
{//com.newland.nxsms.utils.ConfigUtils
	private static final Logger logger = LoggerFactory.getLogger(ConfigUtils.class);

	private ConfigUtils()
	{
		logger.info("Initialize ConfigUtils.......");
	}

	private static class SingletonHolder
	{
		private static final ConfigUtils instance = new ConfigUtils();
	}

	public static ConfigUtils getInstance()
	{
		return SingletonHolder.instance;
	}

	private String loadProperties(String filePath, String key, boolean decrypt)
	{
		String result = null;
		InputStream is = null;
		logger.info("=====filePath====="+filePath+"========key======"+key+"======decrypt======"+decrypt);
		try
		{
			File file = new File(filePath);
			Properties prop = new Properties();
			if (is == null)
			{
				is = new FileInputStream(file);
			}
			prop.load(is);
			result = prop.getProperty(key);
			if (decrypt)
			{
				result = DESCoder.decrypt(result);
			}
		}
		catch (Exception e)
		{
			logger.error(e.getMessage(), e);
		}
		return result;
	}

	public String getValue(String key, boolean decrypt)
	{
//		return loadProperties("conf" + File.separator + "nxsms.properties", key, decrypt);
//		return loadProperties(File.separator +"ftpfile" + File.separator +"application" + File.separator+"sms_wh" + File.separator+"conf" + File.separator+ "nxsms.properties", key, decrypt);
		return loadProperties("D:\\workspace\\sms\\smscard\\VerifySMSP\\conf\\nxsms.properties", key, decrypt);
	}

	public String getValue(String file, String key, boolean decrypt)
	{
		return loadProperties(file, key, decrypt);
	}

	public static void main(String[] args) {
//		System.out.println(DESCoder.decrypt("82C92115D0A21663"));
		System.out.println(DESCoder.encrypt("568646"));
//		System.out.println(DESCoder.encrypt("8855"));
	}
}
